package org.zizi.action.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.zizi.action.ActionContext;
import org.zizi.action.ActionService;
import org.zizi.action.DeleteVatCredit;
import org.zizi.action.GetPreviousLeftCredit;
import org.zizi.action.GetVatCredit;
import org.zizi.action.InvalidVatCredit;
import org.zizi.action.ListManagingVatUserInfo;
import org.zizi.action.SubmitVatCredit;
import org.zizi.action.UserException;
import org.zizi.dao.InlandRevenueOrgDao;
import org.zizi.dao.UserInfoDao;
import org.zizi.dao.VatCreditDao;
import org.zizi.manager.LoginManager;
import org.zizi.manager.Managers;
import org.zizi.manager.VatCreditManager;
import org.zizi.manager.impl.DaoManager;
import org.zizi.modal.InlandRevenueOrg;
import org.zizi.modal.User;
import org.zizi.modal.UserInfo;
import org.zizi.modal.VatCredit;
import org.zizi.modal.VatCreditInvoice;
import org.zizi.util.NumberFormat;

public class VatCreditActionService implements ActionService
{
	private LoginManager lm = Managers.get(LoginManager.class);
	private VatCreditManager vcm = Managers.get(VatCreditManager.class);
	
	private UserInfoDao uiDao = Managers.get(DaoManager.class).getDao(UserInfoDao.class);
	private VatCreditDao vcDao = Managers.get(DaoManager.class).getDao(VatCreditDao.class);
	private InlandRevenueOrgDao iroDao = Managers.get(DaoManager.class).getDao(InlandRevenueOrgDao.class);
	
	public void ListManagingVatUserInfo(ListManagingVatUserInfo action) throws Exception
	{
		if (action.getIdentify() == null || action.getIdentify().isEmpty()) return;
		
		//当前登陆账号
		HttpSession session = ActionContext.get().getSession();
		
		//通过Session取得用户信息。若未取得，则需要重新进入系统
		User u = lm.getUser(session.getId());
		if (u == null) throw new UserException("您尚未登录或登录已过期，请先登录系统！");
		
		InlandRevenueOrg org = iroDao.getInlandRevenueOrg(u.getOrgCode());
		
		List<UserInfo> list = uiDao.searchManagingUserInfos(action.getIdentify(), org.getRelationCode(), 0, action.getLimit());
		for (UserInfo ui : list)
		{
			ui.setOrg(iroDao.getInlandRevenueOrg(ui.getOrgCode()));
		}
		
		action.setList(list);
	}
	
	public void GetPreviousLeftCredit(GetPreviousLeftCredit action) throws Exception
	{
		double money = vcDao.getPreviousLeftCredit(action.getIdentify(), action.getYear(), action.getMonth());
		action.setMoney(money);
	}
	
	public void GetVatCredit(GetVatCredit action) throws Exception
	{
		VatCredit credit = vcDao.getVatCredit(action.getId());
		if (credit == null) return;
		
		credit.setInvoices(vcDao.getVatCreditInvoices(credit.getId()));
		
		UserInfo user = uiDao.getUserInfo(credit.getIdentify());
		if (user != null) user.setOrg(iroDao.getInlandRevenueOrg(user.getOrgCode()));
		
		action.setCredit(credit);
		action.setUser(user);
	}
	
	public void SubmitVatCredit(SubmitVatCredit action) throws Exception
	{
		HttpSession session = ActionContext.get().getSession();
		
		//通过Session取得用户信息。若未取得，则需要重新进入系统
		User u = lm.getUser(session.getId());
		if (u == null) throw new UserException("您尚未登录或登录已过期，请先登录系统！");
		
		Date now = new Date();
		
		//构建发票清单
		VatCredit credit = vcDao.getVatCredit(action.getId());
		if (credit == null)
		{
			credit = new VatCredit();
			credit.setCreatorId(u.getId());
			credit.setSubmitTime(now);
			credit.setSubmitIp(ActionContext.get().getRequest().getRemoteAddr());
		}
		
		credit.setLastModifyId(u.getId());
		credit.setLastModifyTime(now);
		credit.setValid(true);				//编辑以后，一定会重新被生效
		
		credit.setYear(action.getYear());
		credit.setMonth(action.getMonth());
		credit.setIdentify(action.getIdentify());
		
		//发票
		List<VatCreditInvoice> invoices = new ArrayList<VatCreditInvoice>();
		if (action.getInvoices() != null)
		{
			for (VatCreditInvoice s : action.getInvoices())
			{
				if (s.getInvoiceTypeId() == null || s.getInvoiceTypeId() <= 0) continue;
				s.checkInvoiceCategory();
				
				invoices.add(s);
			}
		}
		credit.setInvoices(invoices);
		
		credit.setApplyVatCredit(NumberFormat.format(action.getApplyVatCredit()));
		
		vcm.check(credit, true);	//检查正确性
		
		long creditId = vcDao.addOrUpdateVatCredit(credit);
		vcDao.updateVatCreditInvoices(creditId, invoices);
	}
	
	public void InvalidVatCredit(InvalidVatCredit action) throws Exception
	{
		VatCredit vc = vcDao.getVatCredit(action.getId());
		boolean exist = vcDao.existLatterCredit(vc.getIdentify(), vc.getYear(), vc.getMonth(), vc.getId());
	
		if (exist) throw new UserException("无法废弃此抵减清单：存在比此清单所属期(" + vc.getYear() + "年" + vc.getMonth() + "月)更晚的有效清单。");
	
		vc.setValid(false);
		vcDao.addOrUpdateVatCredit(vc);
	}
	
	public void DeleteVatCredit(DeleteVatCredit action) throws Exception
	{
		VatCredit vc = vcDao.getVatCredit(action.getId());
		if (vc == null) return;
		
		boolean exist = vcDao.existLatterCredit(vc.getIdentify(), vc.getYear(), vc.getMonth(), vc.getId());
		if (exist) throw new UserException("无法删除此抵减清单：存在比此清单所属期(" + vc.getYear() + "年" + vc.getMonth() + "月)更晚的有效清单。");
		
		vcDao.deleteVatCredit(action.getId());
	}
}
